﻿using System;
using System.Collections.Generic;
using Util.Ini;

namespace svnbox {
    class Profile {

        public IniFile ini;
        private  Dictionary<string, string> Defaults = new Dictionary<string, string>() { 
            {"wc.path", "C:\\Svnbox"}
            ,{"svn.repository",""}
            ,{"svn.user",""}
            ,{"svn.password",""}
            ,{"global.language","en"}
            ,{"global.name","default name"}
            ,{"global.startupmonitor","1"}
            ,{"global.desktopnotif","1"}
        };

        private void debug(string s) { System.Diagnostics.Debug.WriteLine("{Profiles} " + s); }

        public Profile() { 
            List<IniFile> Profiles = GetAvailableProfiles();
            if (Profiles.Count == 0) {
                ini = CreateNewProfile(DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"));
            } else {
                foreach (IniFile i in Profiles) {
                    if (i.GetValue("global", "selected","0") == "1"  ) { 
                        ini = i ;
                        break;
                    }
                }
                if (ini == null) {
                    ini = Profiles[Profiles.Count-1];
                }
            }
        }

        //load profile and set as selected 
        //and unselect all others profiles 
        public Profile(string ProfileName) {
            List<IniFile> Profiles = GetAvailableProfiles();
            foreach (IniFile i in Profiles) {
                if (i.GetValue("global", "name") == ProfileName) {
                    i.SetValue("global", "selected", "1");
                    ini = i;
                } else {
                    i.SetValue("global", "selected", "0");
                }
            }
        }

        internal IniFile CreateNewProfile(string ProfileName) {
            IniFile i = new IniFile(App.ProfilsPath + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".ini");

            if (ini != null) {
                foreach (KeyValuePair<string, string> kv in Defaults) {
                    string[] k = kv.Key.Split('.');
                    i.SetValue(k[0],k[1],ini.GetValue(k[0],k[1],kv.Value));
                }
            } else {
                foreach (KeyValuePair<string, string> kv in Defaults) {
                    string[] k = kv.Key.Split('.');
                    i.SetValue(k[0], k[1], kv.Value);
                }
            }
            i.SetValue("global", "name", ProfileName);
            return i;
        }

        private string GetDefault(string section, string key) {
            string output = "";
            if (Defaults.TryGetValue(section + "." + key, out output)) {
                return output;
            } else {
                return "Profile.Defaults.ValueNotSeted (" + section + "." + key + ") !";
            }
        }

        public string Get(string section,string key ){
            return ini.GetValue(section,key,GetDefault(section,key));
        }

        public void Set(string section,string key ,string value ){
            ini.SetValue(section,key,value);
        }

        public List<IniFile> GetAvailableProfiles() {
            List<IniFile> outp = new List<IniFile>();
            string[] AvailableProfils = System.IO.Directory.GetFiles(App.ProfilsPath, "*.ini");
            foreach (string profil in AvailableProfils) {
                IniFile s_ini = new IniFile(profil);
                if (s_ini.GetValue("global", "name", "*****") == "*****") {
                    s_ini.SetValue("global", "name", GetDefault("global", "name"));
                }
                outp.Add(s_ini);
            }
            return outp;
        }
    }
}
